package com.ruoyi.huanghefd.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.huanghefd.domain.Makesheet;
import com.ruoyi.huanghefd.domain.MakesheetDetails;
import com.ruoyi.huanghefd.domain.Worksheet;
import com.ruoyi.huanghefd.mapper.MakesheetDetailsMapper;
import com.ruoyi.huanghefd.service.MakesheetService;
import com.ruoyi.huanghefd.mapper.MakesheetMapper;
import com.ruoyi.huanghefd.utils.PageBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.apache.commons.collections.CollectionUtils;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 *
 */
@Service
public class MakesheetServiceImpl extends ServiceImpl<MakesheetMapper, Makesheet>
    implements MakesheetService{

    private static final Logger log = LoggerFactory.getLogger(MakesheetServiceImpl.class);

    @Resource
    MakesheetMapper makesheetMapper;

    @Resource
    MakesheetDetailsMapper makesheetDetailsMapper;

    @Override
    public AjaxResult addMakesheet(Makesheet makesheets) {
        int result = 0;
        try {
            //操作票主键
            String strMsId =UUID.randomUUID().toString().replace("-", "");
            //操作票票号
            //pubCommon pubCommon = new pubCommon();
            //String strMsCode = pubCommon.getGenerateOrder(strMsTye,"ms");
            String strMsCode = this.getGenerateOrder(makesheets.getMsType());
            log.info("生成操作票号："+strMsCode);
            makesheets.setMsCode(strMsCode);
            makesheets.setMsId(strMsId);

            //插入操作票主表
            log.info("新增操作票-主表开始>>" + makesheets);
            result = makesheetMapper.insertAll(makesheets);
            log.info("新增操作票-主表结束<<" + result);

            //插入操作票明细表
            if(!CollectionUtils.isEmpty(makesheets.getMsdtls())) {

                List<MakesheetDetails> listmsdtl=new ArrayList<>();

                for (MakesheetDetails msdtls : makesheets.getMsdtls()
                ) {
                    String uuid = UUID.randomUUID().toString().replace("-", "");
                    log.info("uuid="+uuid);
                    MakesheetDetails msdtl=new MakesheetDetails();
                    msdtl.setMsId(uuid);
                    msdtl.setMsCode(strMsCode);
                    msdtl.setMsOrder(msdtls.getMsOrder());
                    msdtl.setOperaProject(msdtls.getOperaProject());
                    msdtl.setOperaResult(msdtls.getOperaResult());
                    msdtl.setFinishTime(msdtls.getFinishTime());
                    msdtl.setRemark(msdtls.getRemark());

                    log.info("msdtl="+msdtl);
                    listmsdtl.add(msdtl);
                }
                log.info("新增操作票-明细表开始>>" + listmsdtl);
                result = makesheetDetailsMapper.insertBatch(listmsdtl);
                log.info("新增操作票-明细表结束<<" + result);
            }
            return AjaxResult.success(strMsCode);
        } catch (Exception e) {
            return AjaxResult.error("操作票新增失败", e);
        }
    }

    @Override
    public AjaxResult delMakesheet(String strMsCode) {
        int result = 0;
        try {
            log.info("删除操作票-主表开始>>");
            result = makesheetMapper.delByMsId(strMsCode);
            log.info("删除操作票-主表结束<<"+result);

            log.info("删除操作票-从表开始>>");
            result = makesheetDetailsMapper.delByMsId(strMsCode);
            log.info("删除操作票-从表结束<<"+result);

            return AjaxResult.success(strMsCode);
        } catch (Exception e) {
            return AjaxResult.error("操作票删除失败", e);
        }
    }

    @Override
    public AjaxResult uptMakesheet(Makesheet makesheets) {
        int result = 0;
        try {
            log.info("修改操作票-主表开始>>");
            result = makesheetMapper.updatebyMsId(makesheets);
            log.info("修改操作票-主表结束<<"+result);

            if(!CollectionUtils.isEmpty(makesheets.getMsdtls())){
                log.info("修改操作票-从表开始>>");
                //result = makesheetDetailsMapper.updateBatchByMsId(makesheets.getMsdtls());
                //最好的方式还是先删后插
                makesheetDetailsMapper.delByMsId(makesheets.getMsCode());
                makesheetDetailsMapper.insertBatch(makesheets.getMsdtls());

                log.info("修改操作票-从表结束<<"+result);
            }
            return AjaxResult.success(makesheets.getMsCode());
        } catch (Exception e) {
            return AjaxResult.error("操作票修改失败", e);
        }
    }

    @Override
    public AjaxResult queryMakesheets(String strMsCode, String strMsDept, String strMsState, String strUserId, String strMailType) {
        try {
            List<Makesheet> makesheetList = makesheetMapper.queryMakesheetList(strMsCode, strMsDept,strMsState,strUserId,strMailType);
            if (CollectionUtils.isEmpty(makesheetList)) {
                return AjaxResult.success("操作票查询无数据",null);
            }
            return AjaxResult.success("操作票查询成功",makesheetList);
        } catch (Exception e) {
            return AjaxResult.warn("操作票查询失败",e);
        }
    }

    @Override
    public PageBean queryMakesheetsByPage(String strMsCode, String strMsDept, String strMsState, String strUserId, String strMailType, int pageNum, int pageSize) {
        PageBean pageBean = new PageBean();
        if (pageNum > 0 && pageSize > 0) {
            int totalRecord = makesheetMapper.getMakesheetNum(strMsCode, strMsDept, strMsState, strUserId, strMailType);
            pageBean = new PageBean(pageNum, pageSize, totalRecord);

            //从该行开始拿数据
            int startIndex = (pageNum - 1) * pageSize;
            pageBean.setStartIndex(startIndex);
            pageBean.setCode(200);

            //数据集合
            List<Makesheet> makesheetList = makesheetMapper.queryMakesheetListByPage(strMsCode, strMsDept, strMsState, strUserId, strMailType,startIndex, pageSize);
            pageBean.setList(makesheetList);
            pageBean.setSuccessMsg("操作票查询成功！");
        } else {
            pageBean.setCode(500);
            pageBean.setFailMsg("操作票查询失败！");
        }
        return pageBean;
    }

    /**
     * @Author gnplum
     * @Description //操作票票号
     * @Date 14:56 2023/11/12
     * @param Type
    * @return java.lang.String
    **/
    private String getGenerateOrder(String Type) {

        log.info("获取票编号=" + Type);
        String strWsCode = "";
        int order = 0;
        try {

            order = makesheetMapper.getMsCodeNum(Type);
            log.info("order=" + order);
            //DQ1202311110001
            strWsCode = Type + new SimpleDateFormat("yyyyMMdd").format(new Date()) + String.format("%03d", order);

            log.info("strWsCode=" + strWsCode);

        } catch (Exception e) {
            strWsCode = "";
            //e.printStackTrace();
            //throw  e;
            //log.error(e.getCause()+e.getMessage());
        }
        return strWsCode;
    }
}




